package com.zt.ztyh.admin.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.ztyh.admin.mapper.LoanUserMapper;
import com.zt.ztyh.admin.mapper.UserMapper;
import com.zt.ztyh.admin.service.LoanService;
import com.zt.ztyh.admin.mapper.LoanMapper;
import com.zt.ztyh.model.dto.LoanDto;
import com.zt.ztyh.model.entity.Loan;
import com.zt.ztyh.model.entity.LoanUser;
import com.zt.ztyh.model.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 贾天旺
* @description 针对表【ztyh_loan】的数据库操作Service实现
* @createDate 2024-04-11 18:33:58
*/
@Service
public class LoanServiceImpl extends ServiceImpl<LoanMapper, Loan>
    implements LoanService{


    @Autowired
    private LoanMapper loanMapper;

    @Override
    public Page<Loan> findAllLoan(Integer page, Integer limit, LoanDto loanDto) {
        if(!loanDto.getLname().equals("")){
            return loanMapper.selectPage(new Page<Loan>(page,limit),
                    new QueryWrapper<Loan>().like("lname",loanDto.getLname()));
        }
        if(!loanDto.getCreateTimeBegin().equals("") || !loanDto.getCreateTimeEnd().equals("")){
            return loanMapper.selectPage(new Page<Loan>(page,limit),
                    new QueryWrapper<Loan>().between("createTime",loanDto.getCreateTimeBegin(),loanDto.getCreateTimeEnd()));
        }
        return loanMapper.selectPage(new Page<Loan>(page,limit),new QueryWrapper<Loan>());
    }

    @Override
    public void updateState(Long lid) {
            this.loanMapper.updateState(lid);
    }


    @Autowired
    private LoanUserMapper loanUserMapper;
    @Autowired
    private UserMapper userMapper;
    @Scheduled(cron = "0 0 12 * * ?")
    public void beOverdue() {
        QueryWrapper<LoanUser> state = new QueryWrapper<LoanUser>().eq("state", 2);
        List<LoanUser> loanUsers = loanUserMapper.selectList(state);
        if (loanUsers.size() != 0) {
            for (LoanUser loanUser : loanUsers) {
                User user = userMapper.selectById(loanUser.getUid());
                sendSms(user.getUsername(),user.getPhone(), loanUser.getId().toString(), loanUser.getRedundancy2());
            }
        }
    }


    private void sendSms(String name, String phone, String code, BigDecimal amount) {
        DefaultProfile profile = DefaultProfile.getProfile("default",
                "LTAI5tEuAxSuHHnqCpW8LfK2",
                "VyM1I8aClzm2hSxJf6k1FIOroVXSOv");
        //发起 客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置短信发送的信息 签名 手机号码 验证码
        CommonRequest request = new CommonRequest();
        //设置请求方式
        request.setSysMethod(MethodType.POST);
        //设置阿里云节点
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //设置手机号码 设置成自己的
        request.putQueryParameter("PhoneNumbers", phone);
        //设置签名
        request.putQueryParameter("SignName", "学习是一种生活习惯");
        //设置模板
        request.putQueryParameter("TemplateCode", "SMS_180348176");
        request.putQueryParameter("TemplateParam",
                "尊敬的"+name+"，您当前编号为"+code+"M为："+amount);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("====================================================================="+response.getHttpResponse().isSuccess());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

}




